<?php
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/14 14:36
 * Desc: 景点退款规则
 */
class Service_Ferry_Refund_Rule extends Contract_Refund_Rule
{
    /**
     * 获取格式化的退款规则
     * @param $suit_id
     * @param string $product_id
     * @return array
     */
    public function get_format_refund_rule($suit_id, string $product_id = ''): array
    {
        $rules = $this->get_refund_rule($suit_id);
        if (!empty($rules)) {
            foreach ($rules as &$rule) {
                // 格式化日期
                if ($rule['before_day'] == 0) {
                    $rule['date_des'] = '用户当天 ' . $rule['before_date'] . ' 前';
                } else {
                    $rule['date_des'] = '用户提前 ' . $rule['before_day'] . ' 天 ' . $rule['before_date'] . ' 点前';
                }
                // 格式化金额
                $fee = floatval($rule['fee']);
                if ($rule['fee_type'] == 2) {
                    $fee = Currency_Tool::price($fee);
                }
                $rule['price_des'] = $rule['fee_type'] == 1 ? $fee . '%' : $fee. ' 元';
                $rule['des'] = $rule['date_des'].'申请退款，手续费扣 '.$rule['price_des'];
            }
        }
        return $rules;
    }

    /**
     * 根据套餐id获取退款规则
     * @param $suit_id
     * @param string $product_id
     * @return array
     */
    public function get_refund_rule($suit_id, string $product_id = ''): array
    {
        return DB::select('before_day', 'before_date', 'fee', 'fee_type')
            ->from('ferry_refund_rule')
            ->where('suit_id', '=', $suit_id)
            ->order_by('before_day', 'desc')
            ->order_by('before_date', 'asc')
            ->execute()
            ->as_array();
    }
}